package com.hemaapp.yjnh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseFragment;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.AddressActivity;
import com.hemaapp.yjnh.activity.AfterSalesListActivity;
import com.hemaapp.yjnh.activity.ApplyFarmerActivity0;
import com.hemaapp.yjnh.activity.AttentionActivity;
import com.hemaapp.yjnh.activity.BookOrderListActivity;
import com.hemaapp.yjnh.activity.CollectionActivity;
import com.hemaapp.yjnh.activity.FarmerProgressActivity;
import com.hemaapp.yjnh.activity.FoodStampActivity;
import com.hemaapp.yjnh.activity.FootprintActivity;
import com.hemaapp.yjnh.activity.GuideActivity;
import com.hemaapp.yjnh.activity.MyWalletActivity;
import com.hemaapp.yjnh.activity.MyFarmerActivity;
import com.hemaapp.yjnh.activity.NoticeListActivity;
import com.hemaapp.yjnh.activity.OrderListActivity;
import com.hemaapp.yjnh.activity.QRCordActivity;
import com.hemaapp.yjnh.activity.Register2Activity;
import com.hemaapp.yjnh.activity.SettingActivity;
import com.hemaapp.yjnh.activity.WebviewActivity;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.utils.LoginUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import xtom.frame.util.XtomToastUtil;

public class PersonalFragment extends BaseFragment implements OnClickListener {

    @Bind(R.id.iv_avatar)
    ImageView ivAvatar;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_mobile)
    TextView tvMobile;
//    @Bind(R.id.tv_my_farmer)
//    TextView tvMyFarmer;
    @Bind(R.id.imageView1)
    ImageView imageView1;
    @Bind(R.id.imageView2)
    ImageView imageView2;
    @Bind(R.id.imageView3)
    ImageView imageView3;
    @Bind(R.id.imageView4)
    ImageView imageView4;
    @Bind(R.id.layout_level)
    LinearLayout layoutLevel;
//    @Bind(R.id.tv_notification_new)
//    TextView tvNotificationNew;
    @Bind(R.id.iv_red_point)
    ImageView ivRedPoint;
    private LoginUtil.LoginCallBack loginCallBack;

    private User user;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_personal);
        user = BaseApplication.getInstance().getUser();
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden)
            loginCallBack = null;
        else
            getNetWorker().clientLogin();
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {

    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {

    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_LOGIN:
                User user = ((HemaArrayResult<User>) baseResult).getObjects().get(0);
                BaseApplication.getInstance().setUser(user);
                this.user = user;
                checkUnreadMsg();
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_LOGIN:
                checkUnreadMsg();
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case CLIENT_LOGIN:
                checkUnreadMsg();
                break;
        }
    }

    @Override
    protected void findView() {
    }

    @Override
    protected void setListener() {
    }

    /**
     * 设置兔子等级
     * <p/>
     * 0-49	铁兔子	1
     * 50-99	铁兔子	2
     * 100-199	铁兔子	3
     * 200-399	铁兔子	4
     * 400-799	铜兔子	1
     * 800-1599	铜兔子	2
     * 1600-3199	铜兔子	3
     * 3200-6399	铜兔子	4
     * 6400-12799	银兔子	1
     * 12800-25599	银兔子	2
     * 25600-51199	银兔子	3
     * 51200-102399	银兔子	4
     * 102400-204799	金兔子	1
     * 204800-409599	金兔子	2
     * 409600-819199	金兔子	3
     * 819200-1638399	金兔子	4
     * 1638400-3276799	蓝钻兔子	1
     * 3276800-6553599	蓝钻兔子	2
     * 6553600-13107199	蓝钻兔子	3
     * 13107200-26214399	蓝钻兔子	4
     * 26214400-52428799
     * 52428800-104857599
     * 104857600-209715199
     * 209715200-419430399
     * 419430400及以上
     */
    private void setGrade() {
        imageView1.setVisibility(View.GONE);
        imageView2.setVisibility(View.GONE);
        imageView3.setVisibility(View.GONE);
        imageView4.setVisibility(View.GONE);
        String level = user.getLevel_score();
        double score = 0;
        try {
            score = Double.parseDouble(level);
        } catch (Exception e) {
            XtomToastUtil.showShortToast(getActivity(), "获取等级失败");
            return;
        }
//		if (TextUtils.isDigitsOnly(level)) {
//			int score = Integer.parseInt(level);
        //铁兔子
        if (score < 50) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_1);
        } else if (score < 100) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_1);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_1);
        } else if (score < 200) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_1);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_1);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_1);
        } else if (score < 400) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_1);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_1);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_1);
            imageView4.setVisibility(View.VISIBLE);
            imageView4.setImageResource(R.drawable.rabbit_1);
            //铜兔子
        } else if (score < 800) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_2);
        } else if (score < 1600) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_2);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_2);
        } else if (score < 3200) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_2);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_2);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_2);
        } else if (score < 6400) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_2);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_2);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_2);
            imageView4.setVisibility(View.VISIBLE);
            imageView4.setImageResource(R.drawable.rabbit_2);
            //银
        } else if (score < 12800) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_3);
        } else if (score < 25600) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_3);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_3);
        } else if (score < 51200) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_3);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_3);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_3);
        } else if (score < 102400) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_3);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_3);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_3);
            imageView4.setVisibility(View.VISIBLE);
            imageView4.setImageResource(R.drawable.rabbit_3);
            //金
        } else if (score < 204800) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_4);
        } else if (score < 409600) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_4);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_4);
        } else if (score < 819200) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_4);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_4);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_4);
        } else if (score < 1638400) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_4);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_4);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_4);
            imageView4.setVisibility(View.VISIBLE);
            imageView4.setImageResource(R.drawable.rabbit_4);
            //蓝钻
        } else if (score < 327680) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_5);
        } else if (score < 6553600) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_5);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_5);
        } else if (score < 13107200) {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_5);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_5);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_5);
        } else {
            imageView1.setVisibility(View.VISIBLE);
            imageView1.setImageResource(R.drawable.rabbit_5);
            imageView2.setVisibility(View.VISIBLE);
            imageView2.setImageResource(R.drawable.rabbit_5);
            imageView3.setVisibility(View.VISIBLE);
            imageView3.setImageResource(R.drawable.rabbit_5);
            imageView4.setVisibility(View.VISIBLE);
            imageView4.setImageResource(R.drawable.rabbit_5);
        }
//		}
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_avatar, R.id.tv_name, R.id.tv_my_farmer, R.id.tv_shopping_cart,
            R.id.tv_collection, R.id.tv_attention, R.id.tv_foot, R.id.tv_order, R.id.tv_custom_made,
            R.id.tv_bespeak, R.id.tv_after_sales, R.id.tv_wallet, R.id.tv_coupon, R.id.layout_qr,
            R.id.layout_notification, R.id.tv_address, R.id.tv_settting, R.id.layout_level, R.id.tv_help})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_avatar:
            case R.id.tv_name:
                if (user != null) {
                    Intent intent = new Intent(getActivity(), Register2Activity.class);
                    intent.putExtra("type", 2);
                    startActivity(intent);
                }else
                    BaseUtil.checkLogin(getActivity());
                break;
            case R.id.tv_my_farmer:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
//                        Intent intent = new Intent(getActivity(), MyFarmerActivity.class);
//                        startActivity(intent);
                        Intent itFarmer = new Intent();
                        if (user.getRole().equals("1")) {//角色	0普通用户 1田家用户
                            itFarmer.setClass(getActivity(), MyFarmerActivity.class);
                        } else if (user.getAuthflag().equals("1")) {//审核中的普通用户
                            itFarmer.setClass(getActivity(), FarmerProgressActivity.class);
                        } else {//普通田家
                            itFarmer.setClass(getActivity(), ApplyFarmerActivity0.class);
                        }
                        startActivity(itFarmer);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.layout_level://兔子等级说明
                Intent intent = new Intent(getActivity(), WebviewActivity.class);
                intent.putExtra("parm", "rabbit_level");
                intent.putExtra("type", 11);
                startActivity(intent);
                break;
            case R.id.tv_shopping_cart:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent intent = new Intent(getActivity(), WebviewActivity.class);
                        intent.putExtra("type", 5);
                        startActivity(intent);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_collection:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent intent = new Intent(getActivity(), CollectionActivity.class);
                        startActivity(intent);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_attention:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), AttentionActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_foot:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), FootprintActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_order:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itOrder = new Intent(getActivity(), OrderListActivity.class);
                        itOrder.putExtra("billtype", "1");
                        startActivity(itOrder);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_custom_made:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itOrder = new Intent(getActivity(), OrderListActivity.class);
                        itOrder.putExtra("billtype", "2");
                        startActivity(itOrder);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_bespeak:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), BookOrderListActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_after_sales:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), AfterSalesListActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_wallet:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), MyWalletActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_coupon:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), FoodStampActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.layout_qr:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent itMessage = new Intent(getActivity(), QRCordActivity.class);
                        startActivity(itMessage);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.layout_notification:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent intent = new Intent(getActivity(), NoticeListActivity.class);
                        startActivity(intent);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_address:
                loginCallBack = new LoginUtil.LoginCallBack() {
                    @Override
                    public void onLogin() {
                        Intent intent = new Intent(getActivity(), AddressActivity.class);
                        intent.putExtra("type", 1);
                        startActivity(intent);
                        loginCallBack = null;
                    }
                };
                LoginUtil.getInstance(getActivity(), loginCallBack);
                break;
            case R.id.tv_settting:
                Intent itSet = new Intent(getActivity(), SettingActivity.class);
                startActivity(itSet);
                break;
            case R.id.tv_help:
                Intent itGuide = new Intent(getActivity(), GuideActivity.class);
                startActivity(itGuide);
                break;
        }
    }

    private void initViews() {
        if (user == null) {
            ivRedPoint.setVisibility(View.INVISIBLE);
            ivAvatar.setImageResource(R.drawable.default_rabbit_grey);
            tvName.setText("请登录");
            tvMobile.setVisibility(View.GONE);
//            tvMyFarmer.setVisibility(View.GONE);
            layoutLevel.setVisibility(View.GONE);
        }else {
            setGrade();
            ImageUtils.loadAvatar(getActivity(), user.getAvatar(), ivAvatar);
            tvName.setText(user.getNickname());
            tvMobile.setVisibility(View.VISIBLE);
            tvMobile.setText("("+user.getUsername()+")");
//            tvMyFarmer.setVisibility(View.VISIBLE);
//            //如果登录用户是已经通过了田家审核，就在右上角显示“我的田家”,否则不显示
//            //角色	0普通用户 1田家用户
//            if (user != null && user.getRole().equals("1")) {
//                tvMyFarmer.setVisibility(View.VISIBLE);
//            } else {
//                tvMyFarmer.setVisibility(View.GONE);
//            }
            layoutLevel.setVisibility(View.VISIBLE);
            checkUnreadMsg();
        }
    }


    /**判断是否有未读消息
     *
     */
    private void checkUnreadMsg() {
        if (user != null && !isNull(user.getNoticecount()) && !user.getNoticecount().equals("0")) {
            ivRedPoint.setVisibility(View.VISIBLE);
        } else {
            ivRedPoint.setVisibility(View.INVISIBLE);
        }
    }

    @Subscribe
    public void onEventMainThread(EventBusMsg msg) {
        if (msg.getType() == EventBusConfig.USER) {
            this.user = msg.getUser();
            initViews();
            if (loginCallBack != null)
                loginCallBack.onLogin();
        }else if (msg.getType() == EventBusConfig.NEW_MSG) {//有新消息
            ivRedPoint.setVisibility(View.VISIBLE);
        }
    }
}
